import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:shared_components/build-common/buildCommon.dart';
import 'package:shared_components/build-image/buildImage.dart';
import 'package:shared_components/build-line/buildLine.dart';

getStateName(String code, dynamic item) {
  var map = {
    "DFK": "待付款",
    "DFW": "待服务",
    "DWC": "待完成",
    "YWC": "已完成",
    "YQX": "已取消",
    "YSH": "售后"
  };

  if (item["refundDone"].toString() == "1" &&
      (code == "YSH" || code == "YWC" || code == 'JXZ')) {
    return "已售后";
  }

  if (item["refundDone"].toString() != "1" &&
      (code == "YSH" || code == "YWC" || code == 'JXZ')) {
    // return "待售后";
    return getStatusName(showText(item["orderRefundStatus"]), "");
  }
  // if (item["refundDone"].toString() == "1" && code == "YSH") {
  //   return "已售后";
  // }

  return showText(map[code]);
}

Widget buildSHCard(
    {dynamic item,
    String code = '',
    Function? handleClick,
    String type = 'business',
    Function? makeUpPayFn,
    Function? handleCommon,
    required Function handleFn}) {
  return GestureDetector(
    onTap: () {
      if (handleClick != null) {
        handleClick();
      }
    },
    child: Container(
        margin: const EdgeInsets.only(top: 10, bottom: 5),
        decoration: const BoxDecoration(
          color: Colors.white, //圆角
        ),
        child: Column(children: [
          Container(
            width: double.infinity,
            // height: 180,
            child: Column(
              children: [
                // 订单和状态
                Container(
                  width: double.infinity,
                  height: 50,
                  // color: Colors.blue,
                  child: Row(
                    children: [
                      Expanded(
                          child: Container(
                        width: double.infinity,
                        height: double.infinity,
                        // color: Colors.teal,
                        alignment: Alignment.centerLeft,
                        padding: const EdgeInsets.only(left: 20),
                        child: Text(
                          item["orderNo"].toString(),
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                          style: const TextStyle(
                              color: Color(0xff7E7D83), fontSize: 14),
                        ),
                      )),
                      Container(
                        width: 100,
                        height: double.infinity,
                        // color: Colors.green,
                        alignment: Alignment.center,
                        child: Text(
                          showText(item["orderRefundStatusShow"]),
                          //  child: Text(
                          // getStateName(item["state"].toString(), item),

                          // "待付款",
                          style: const TextStyle(
                              fontSize: 13,
                              color: Colors.black,
                              fontWeight: FontWeight.bold),
                        ),
                      )
                    ],
                  ),
                ),
                buildLine(),

                Container(
                  width: double.infinity,
                  height: 100,
                  child: Row(
                    children: [
                      Visibility(
                        // visible: item["orderType"].toString() == "SHOP",
                        child: Container(
                            width: 110,
                            height: double.infinity,
                            alignment: Alignment.center,
                            child: SizedBox(
                              width: 80,
                              height: 80,
                              child: buildImage(
                                  type: "ClipRect",
                                  url: item["imgUrl"].toString(),
                                  fit: BoxFit.fill),
                            )),
                      ),
                      Expanded(
                          flex: 1,
                          child: SizedBox(
                            width: double.infinity,
                            height: double.infinity,
                            child: Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 10),
                              child: Column(
                                children: [
                                  Expanded(
                                      flex: 1,
                                      child: SizedBox(
                                        width: double.infinity,
                                        height: double.infinity,
                                        child: Align(
                                          alignment: Alignment.centerLeft,
                                          child: Text(
                                            showText(item["productName"]),
                                            textAlign: TextAlign.center,
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                            style: const TextStyle(
                                                fontSize: 15,
                                                fontWeight: FontWeight.w500,
                                                color: Color(0xff1D1D1F)),
                                          ),
                                        ),
                                      )),
                                  Expanded(
                                    flex: 1,
                                    child: Container(
                                      width: double.infinity,
                                      height: double.infinity,
                                      padding: const EdgeInsets.only(
                                          right: 20, bottom: 10),
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.end,
                                        children: [
                                          Text(
                                            "¥" +
                                                item["productPrice"].toString(),
                                            style: const TextStyle(
                                                fontWeight: FontWeight.bold,
                                                color: Color(0xffF03C36),
                                                fontSize: 18),
                                          ),
                                          Text(
                                            "x${item["quantity"]}  ",
                                            style: const TextStyle(
                                                color: Color(0xff999999)),
                                          )
                                        ],
                                      ),
                                    ),
                                  )
                                ],
                              ),
                            ),
                          )),
                    ],
                  ),
                ),

                // ...controller.getOrderItems(item).map((itemDetail) {
                //   return buildItem(item, itemDetail);
                // }),
                buildLine()
              ],
            ),
          ),
          Container(
              width: double.infinity,
              // height: 50,
              padding: EdgeInsets.only(left: 30, right: 30, bottom: 5, top: 5),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Visibility(
                      visible: (item["state"] == "YSH" ||
                              item["state"] == "YWC" ||
                              item["state"] == "JXZ") &&
                          item["orderRefundStatus"] == "5" &&
                          type == 'user',
                      child: GestureDetector(
                          onTap: () async {
                            if (makeUpPayFn != null) {
                              makeUpPayFn();
                            }
                            // handleFn();
                          },
                          child: Container(
                            width: double.infinity,
                            height: 35,
                            // margin: const EdgeInsets.only(bottom: 10, right: 8),
                            decoration: const BoxDecoration(
                              //边框
                              color: Color(0xffdf8e46),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10.0)), //圆角
                            ),
                            alignment: Alignment.center,
                            child: const Text(
                              "补差价",
                              style:
                                  TextStyle(color: Colors.white, fontSize: 13),
                            ),
                          ))),
                  Visibility(
                      visible: (item["state"] == "YSH" ||
                              item["state"] == "YWC" ||
                              item["state"] == "JXZ") &&
                          item["orderRefundStatus"] == "6" &&
                          type == 'business',
                      child: GestureDetector(
                          onTap: () async {
                            if (handleCommon != null) {
                              handleCommon('fahuo');
                            }
                            // handleFn();
                          },
                          child: Container(
                            width: double.infinity,
                            height: 35,
                            // margin: const EdgeInsets.only(bottom: 10, right: 8),
                            decoration: const BoxDecoration(
                              //边框
                              color: Color(0xffdf8e46),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10.0)), //圆角
                            ),
                            alignment: Alignment.center,
                            child: const Text(
                              "发货",
                              style:
                                  TextStyle(color: Colors.white, fontSize: 13),
                            ),
                          ))),
                  Visibility(
                      visible: (item["state"] == "YSH" ||
                              item["state"] == "YWC" ||
                              item["state"] == "JXZ") &&
                          item["orderRefundStatus"] == "3" &&
                          type == 'business',
                      child: GestureDetector(
                          onTap: () async {
                            if (handleCommon != null) {
                              handleCommon('shouhuo');
                            }
                            // handleFn();
                          },
                          child: Container(
                            width: double.infinity,
                            height: 35,
                            // margin: const EdgeInsets.only(bottom: 10, right: 8),
                            decoration: const BoxDecoration(
                              //边框
                              color: Color(0xffdf8e46),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10.0)), //圆角
                            ),
                            alignment: Alignment.center,
                            child: const Text(
                              "确认收货",
                              style:
                                  TextStyle(color: Colors.white, fontSize: 13),
                            ),
                          ))),
                  Visibility(
                      visible: (item["state"] == "YSH" ||
                              item["state"] == "YWC" ||
                              item["state"] == "JXZ") &&
                          item["orderRefundStatus"] == "8" &&
                          type == 'user',
                      child: GestureDetector(
                          onTap: () async {
                            if (handleCommon != null) {
                              handleCommon('shouhuo');
                            }
                            // handleFn();
                          },
                          child: Container(
                            width: double.infinity,
                            height: 35,
                            // margin: const EdgeInsets.only(bottom: 10, right: 8),
                            decoration: const BoxDecoration(
                              //边框
                              color: Color(0xffdf8e46),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10.0)), //圆角
                            ),
                            alignment: Alignment.center,
                            child: const Text(
                              "确认收货",
                              style:
                                  TextStyle(color: Colors.white, fontSize: 13),
                            ),
                          ))),
                  Visibility(
                      visible: (item["state"] == "YSH" ||
                              item["state"] == "YWC" ||
                              item["state"] == "JXZ") &&
                          item["orderRefundStatus"] == "4" &&
                          type == 'business',
                      child: GestureDetector(
                          onTap: () async {
                            handleFn();
                          },
                          child: Container(
                            width: double.infinity,
                            height: 35,
                            // margin: const EdgeInsets.only(bottom: 10, right: 8),
                            decoration: const BoxDecoration(
                              //边框
                              color: Color(0xffdf8e46),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10.0)), //圆角
                            ),
                            alignment: Alignment.center,
                            child: const Text(
                              "处理",
                              style:
                                  TextStyle(color: Colors.white, fontSize: 13),
                            ),
                          ))),
                  Visibility(
                      visible: (item["state"] == "YSH" ||
                              item["state"] == "YWC" ||
                              item["state"] == "JXZ") &&
                          item["orderRefundStatus"] == "0" &&
                          showText(item["refundDone"]) == "0" &&
                          type == 'business',
                      child: GestureDetector(
                          onTap: () async {
                            handleFn();
                          },
                          child: Container(
                            width: double.infinity,
                            height: 35,
                            // margin: const EdgeInsets.only(bottom: 10, right: 8),
                            decoration: const BoxDecoration(
                              //边框
                              color: Color(0xffdf8e46),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10.0)), //圆角
                            ),
                            alignment: Alignment.center,
                            child: const Text(
                              "处理",
                              style:
                                  TextStyle(color: Colors.white, fontSize: 13),
                            ),
                          ))),
                  // Visibility(
                  //     visible: item["orderRefundStatus"] != "0",
                  //     child: Text(getStatusName(
                  //         showText(item["orderRefundStatus"]), type)))
                ],
              ))
        ])),
  );
}

String getStatusName(String status, String type) {
  var res = "";
  switch (status) {
    case "0":
      res = "待处理";
      break;
    case "2":
      res = "已拒绝";
      break;
    case "3":
      res = "商家待收货";
      break;
    case "4":
      res = "商家已收货";
      break;
    case "5":
      res = "补差价";
      if (type == 'user') {
        res = "";
      }
      break;
    case "6":
      res = " 等待商家发货";
      if (type == 'business') {
        res = "";
      }
      break;
    case "9":
      res = "已售后";
      break;
    default:
  }

  return res;
}
